import { VideoService } from '@/lib/video/video-service';
import { NextRequest, NextResponse } from 'next/server';

/**
 * GET /api/videos/newest - 获取最新视频API
 * 支持以下查询参数：
 * - page: 页码
 */
export async function GET(request: NextRequest) {
  try {
    // 解析URL查询参数
    const { searchParams } = new URL(request.url);
    console.log('[API] 收到最新视频请求参数:', Object.fromEntries(searchParams.entries()));
    
    // 获取页码参数
    const page = parseInt(searchParams.get('page') || '1');
    
    console.log('[API] 解析最新视频参数:', { page });
    
    // 每页显示的数量固定为24个
    const pageSize = 24;
    
    // 调用视频服务获取最近24小时内的数据
    const { videos, total } = await VideoService.getLast24HoursVideos(page, pageSize);
    
    // 计算总页数
    const totalPages = Math.ceil(total / pageSize);
    const hasMore = page < totalPages;
    
    console.log('[API] 返回最新视频数据:', { 
      videoCount: videos.length, 
      total, 
      currentPage: page, 
      totalPages,
      hasMore
    });
    
    // 返回结果
    return NextResponse.json({ 
      videos, 
      total, 
      currentPage: page, 
      totalPages,
      hasMore
    });
  } catch (error) {
    console.error('[API] 最新视频服务器错误:', error);
    return NextResponse.json({ error: '服务器内部错误', details: (error as Error).message, videos: [], total: 0 }, { status: 500 });
  }
} 